using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Citrix.Cmp;

namespace SkinningSample.ViewModel
{
    /// <summary>
    /// This is the view-model object for the Page1 view.
    /// </summary>
    class Page1ViewModel : INotifyPropertyChanged, IPage
    {
        /// <summary>
        /// A reference back to the root v-m object.
        /// </summary>
        private readonly ViewModelManager _vmRoot;

        /// <summary>
        /// Whether we are auto detecting the skin based on the client type.
        /// </summary>
        private bool _isAutoSkin = true;

        /// <summary>
        /// Whether we have overridden the skin to the Android skin.
        /// </summary>
        private bool _overrideToAndroidSkin = false;

        /// <summary>
        /// Whether we have overridden the skin to the iOS skin.
        /// </summary>
        private bool _overrideToIOSSkin = false;

        /// <summary>
        /// Initialisation constructor.
        /// </summary>
        /// <param name="vmMgr">The root view-model object.</param>
        /// <exception cref="ArgumentNullException"></exception>
        public Page1ViewModel(ViewModelManager vmMgr)
        {
            if (null == vmMgr)
            {
                throw new ArgumentNullException("vmMgr");
            }

            _vmRoot = vmMgr;

            PowerCommand = new DelegateCommand(() => _vmRoot.App.Shutdown());
            ShowBooksCommand = new DelegateCommand(() => _vmRoot.NavigateToPage(new Page2ViewModel(_vmRoot)));
        }

        /// <summary>
        /// The power command closes the application.
        /// </summary>
        public DelegateCommand PowerCommand
        {
            get;
            private set;
        }

        /// <summary>
        /// This command displays another page (Page2) which shows a list of books.
        /// </summary>
        public DelegateCommand ShowBooksCommand
        {
            get;
            private set;
        }

        /// <summary>
        /// Set to true to make the app auto-detect which skin to apply based on the type of the
        /// client the app is being accessed from.
        /// </summary>
        public bool IsAutoSkin
        {
            get
            {
                return _isAutoSkin;
            }
            set
            {
                if (value != _isAutoSkin)
                {
                    _isAutoSkin = value;
                    if (_isAutoSkin)
                    {
                        _vmRoot.SkinManager.SkinMode = Skinning.SkinOverrideMode.Auto;
                    }
                }
            }
        }

        /// <summary>
        /// Set to true to force the app to use the Android skin.
        /// </summary>
        public bool IsAndroidSkin
        {
            get
            {
                return _overrideToAndroidSkin;
            }
            set
            {
                if (value != _overrideToAndroidSkin)
                {
                    _overrideToAndroidSkin = value;
                    if (_overrideToAndroidSkin)
                    {
                        _vmRoot.SkinManager.SkinMode = Skinning.SkinOverrideMode.Android;
                    }
                }
            }
        }

        /// <summary>
        /// Set to true to force the app to use the iOS skin.
        /// </summary>
        public bool IsIOSSkin
        {
            get
            {
                return _overrideToIOSSkin;
            }
            set
            {
                if (value != _overrideToIOSSkin)
                {
                    _overrideToIOSSkin = value;
                    if (_overrideToIOSSkin)
                    {
                        _vmRoot.SkinManager.SkinMode = Skinning.SkinOverrideMode.iOS;
                    }
                }
            }
        }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Fired whenever a public property changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Fires the property change event.
        /// </summary>
        /// <param name="propertyName">The name of the property that changed.</param>
        private void OnPropertyChanged(string propertyName)
        {
            NotifyHelper.VerifyProperty(propertyName, this);
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Raise(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion // INotifyPropertyChanged Members

        #region IPage Members

        /// <summary>
        /// Used to notify the page when is made the active page, or when it has become inactive.
        /// </summary>
        /// <param name="isActive">True if this page is now active, false if it has become
        /// inactive.</param>
        public void NotifyActiveChanged(bool isActive)
        {
        }

        /// <summary>
        /// Notifies the active page that the physical back button has been pressed on the
        /// client device.
        /// </summary>
        public void NotifyBackButtonPressed()
        {
        }

        /// <summary>
        /// Used to access the back command that will navigate you from the current page to the
        /// previous page in the navigation history.
        /// </summary>
        public ViewModelManager VmManager
        {
            get { return this._vmRoot; }
        }

        #endregion
    }
}
